package org.feerepository.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class Application extends Composite
{
	public interface ApplicationListener
	{
		void onMainMenuItemSelected(TreeItem selectedItem);
	}
	
	private HorizontalPanel linksPanel;
	private FlexTable topPanel;
	private HorizontalPanel bottomPanel;
	private final static String DEFAULT_STYLE_NAME = "Application";
	private Tree mainMenu;
	private ApplicationListener commandListener;
	private Grid contentLayout;
	private SimplePanel contentWraper;
	public Application()
	{
		FlowPanel layout = new FlowPanel();
		initWidget(layout);
		createTopPanel();
		bottomPanel = new HorizontalPanel();
		bottomPanel.setWidth("100%");
		bottomPanel.setSpacing(0);
		
		createMainMenu();
		bottomPanel.add(mainMenu);
		
		layout.add(topPanel);
		layout.add(bottomPanel);
		
		//setup the Content panel
		contentLayout = new Grid(2,1);
		contentLayout.setCellPadding(0);
		contentLayout.setCellSpacing(0);
		bottomPanel.add(contentLayout);
		
		CellFormatter contentFormatter = contentLayout.getCellFormatter();
		setContentTitle(new HTML("Insert here the content title"));
		contentFormatter.setStyleName(0, 0, DEFAULT_STYLE_NAME+"-content-title");
		
		contentWraper = new SimplePanel();
		contentLayout.setWidget(1, 0, contentWraper);
		contentFormatter.setStyleName(1, 0, DEFAULT_STYLE_NAME + "-content-wraper");
		
		setContent(null);
		
	}
	
	protected void createTopPanel()
	{
		linksPanel = new HorizontalPanel();
		topPanel = new FlexTable();
		topPanel.setCellPadding(0);
		topPanel.setCellSpacing(0);
		topPanel.setWidget(0, 0, linksPanel);
		FlexCellFormatter topFormater = topPanel.getFlexCellFormatter();
		topFormater.setStyleName(0, 0, DEFAULT_STYLE_NAME + "-links");
		topFormater.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		topFormater.setColSpan(0, 0, 2);
		
		setTitleWidget(null);
		topFormater.setStyleName(1, 0, DEFAULT_STYLE_NAME + "-title");
		
    	topPanel.setStyleName(DEFAULT_STYLE_NAME + "-top");
		topPanel.getRowFormatter().setVerticalAlign(0, HasVerticalAlignment.ALIGN_TOP);
		topPanel.getRowFormatter().setVerticalAlign(1, HasVerticalAlignment.ALIGN_TOP);


/*create search bar panel*/
        HorizontalPanel search = new HorizontalPanel();
        search.setStyleName("Application-search");
        search.setWidth("100%");
        topPanel.setWidget(2, 0, search);
        topFormater.setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
        search.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        search.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        HorizontalPanel container = new HorizontalPanel();
        container.setSpacing(2);
        container.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        container.add(ApplicationComponentFactory.getHiperlink("Advanced search"));
        container.add(new HTML("&nbsp;|&nbsp;"));
        container.add(new HTML("Search:"));
        container.add(ApplicationComponentFactory.getSearchBox());
        container.add(ApplicationComponentFactory.getButton("Go"));
        search.add(container);
 /*info panel*/

        HorizontalPanel info = new HorizontalPanel();
        
        info.add(new HTML("Friday, 26 September 2006"));
        info.setWidth("300px");
        HorizontalPanel loginArea = new HorizontalPanel();
        loginArea.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        loginArea.add(new HTML("Welcome <b>Oleg B</b>"));
        loginArea.add(new HTML("&nbsp;|&nbsp;"));
        loginArea.add(ApplicationComponentFactory.getHiperlink("[Log Out]"));
        Grid allData = new Grid(1,2);
        allData.setCellPadding(0);
        allData.setCellSpacing(0);
        allData.setWidth("100%");
        allData.getCellFormatter().setStyleName(0, 1, "LoginArea");
        allData.getCellFormatter().setAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT,
                                                        HasVerticalAlignment.ALIGN_MIDDLE);
        allData.setWidget(0, 0, info);
        allData.setWidget(0, 1, loginArea);
        
        topPanel.setWidget(3, 0, allData);


        
	}
	
	public Tree getMainMenu() {
		return mainMenu;
	}

	public void setMainMenu(Tree mainMenu) {
		this.mainMenu = mainMenu;
	}

	public static String getDEFAULT_STYLE_NAME() {
		return DEFAULT_STYLE_NAME;
	}

	public void setApplicationCommandListener(ApplicationListener listener)
	{
		this.commandListener = listener;
	}
	
	protected void createMainMenu()
	{
		mainMenu =  ApplicationComponentFactory.getMainMenu();
		mainMenu.setAnimationEnabled(true);
		mainMenu.addStyleName(DEFAULT_STYLE_NAME + "-menu");
		
		mainMenu.addTreeListener(new TreeListener()
		{

			public void onTreeItemSelected(TreeItem item) {
				if(commandListener != null)
				{
					commandListener.onMainMenuItemSelected(item);
				}
				
			}

			public void onTreeItemStateChanged(TreeItem item) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	public void setTitleWidget(Widget widget)
	{
		topPanel.setWidget(1, 0, widget);
	}
	

	
	public void addApplicationLink(Widget link)
	{
		if(linksPanel.getWidgetCount() > 0)
		{
            HTML separator = new HTML("&nbsp;|&nbsp;");
            separator.addStyleName("Application-links");
			linksPanel.add(separator);
		}
			linksPanel.add(link);
	}
	
	public void setContentTitle(Widget title)
	{
		contentLayout.setWidget(0, 0, title);
	}
	
	public void setContent(Widget content)
	{
		if(content == null)
		{
			contentWraper.add(new HTML("&nbsp;"));
		}
		else 
			contentWraper.setWidget(content);
	}
}
